package me.yricky.alia.data.base

import kotlin.math.acos
import kotlin.math.sqrt

/**
 * @author Yricky
 * @date 2022/1/27
 */
data class Coord(
    val x:Int,
    val y:Int
){
    operator fun minus(that:Coord):Coord{
        return Coord(x-that.x,y-that.y)
    }

    operator fun plus(that: Coord):Coord{
        return Coord(x+that.x,y+that.y)
    }

    fun divide(that:Int):Coord{
        return Coord(x/that,y/that)
    }

    fun multiply(that: Int):Coord{
        return Coord(x*that,y*that)
    }

    infix fun dotMul(that: Coord):Int{
        return x*that.x + y*that.y
    }

    fun abs():Double = sqrt(x*1.0*x+y*1.0*y)

    override fun toString(): String {
        return "($x,$y)"
    }

    companion object{
        val ORIGIN = Coord(0,0)
        val X_UNIT = Coord(1,0)
        val Y_UNIT = Coord(0,1)
    }
}

fun distance(c1:Coord,c2:Coord):Double{
    return sqrt(1.0*(c1.x-c2.x)*(c1.x-c2.x) + 1.0*(c1.y-c2.y)*(c1.y-c2.y))
}

fun angleRad(edge1:Coord,vertex:Coord,edge2:Coord):Double{
    val vec1 = edge1-vertex
    val vec2 = edge2-vertex
    return angleRad(vec1, vec2)
}

fun angleRad(vec1:Coord,vec2:Coord):Double{
    return acos((vec1 dotMul vec2)/vec1.abs()/vec2.abs())
}

